package org.example.config;

import lombok.extern.slf4j.Slf4j;
import org.example.interceptor.SafeInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;

/**
 * @author liyishan
 * @date 2023/4/18 15:45
 */
@Slf4j
@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * 跨域支持
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600 * 24);
    }
    public static final List<String> WHITE_URL_01 = Arrays.asList(
            "/beta/cms/finance/mbk/virtual/subaccount/cash",
            "/cms/sfexpress/route/back",
            "/api/pay/resultCallback",
            "/lianlian/selectMany",
            "/lianlian/signInsert",
            "/api/pay/ch/resultCallback",
            "/api/gqc/cooperator/check",
            "/big/dataCenterDetail",
            "/big/dataCenterParam",
            "/big/dataCenterSignDetail",
            "/big/dataCenterSignParam",
            "/big/dataCenterMapDetail",
            "/big/dataCenterMapParam",
            "/zhongxue/tastInterest",
            "/big/industryMapParam",
            "/big/industryMapDetail",
            "/api/tast/kcallback",
            "/cms/alipay01/callback/single/withdraw/notify",
            "/cms/alipay02/callback/single/withdraw/notify",
            "/cms/alipay03/callback/single/withdraw/notify",
            "/cms/alipay51/callback/single/withdraw/notify",
            "/cms/alipay52/callback/single/withdraw/notify",
            "/cms/alipay53/callback/single/withdraw/notify",
            "/cms/hyg/util/find/card/info/by/bank/card",
            "/local/test/portrait/separation",
            "/local/test/portrait/separation/single",
            "/whiteness/callback/wzcb/**",
            "/whiteness/callback/pabk/**",
            "/whiteness/callback/zhongan/**",
            "/whiteness/callback/letsign/**",
            "/test/task/**",
            "/cms/cmbchian2/callback/distribute/results/notify",
            "/cms/cmbchian3/callback/distribute/results/notify",
            "/online/invoice/result/callback",
            "/manual/send/mq",
            "/local/test/invoice/remind"
    );

    public static final List<String> WHITE_URL_02 = Arrays.asList(
            "/login",
            "/init",
            "/validation",
            "/banPai/recharge/success/callback",
            "/banPai/cash/success/callback",
            "/banPai/batch/refund/ticket/callback",
            "/cooperator/delivery/industry/type",
            "/update/cooperator/industry"
    );

    /**
     * 拦截器 做了跨域处理 & 跳过权限校验
     */
    public static final List<String> WHITE_URL_03 = Arrays.asList(
            "/logout",
            "/tianyan",
            "/home/get/business/user",
            "/cms/find/deparment/user"

    );

    /**
     * 注入拦截器
     * @return
     */
    @Bean
    public SafeInterceptor safeInterceptor() {
        return new SafeInterceptor();
    }

    /**
     * 添加拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("Interceptor add(init). -{}-", System.currentTimeMillis());
        registry.addInterceptor(safeInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(WHITE_URL_01)
                .excludePathPatterns(WHITE_URL_02);
        log.info("Interceptor-added(over). -{}-", System.currentTimeMillis());
    }


    /**
     * 路径匹配器
     */
    private static final AntPathMatcher ANT_PATH_MATCHER = new AntPathMatcher();

    /**
     * URI匹配
     *
     * @return 匹配结果
     */
    public static boolean match(List<String> patterns, String path) {
        return patterns.stream().anyMatch(pattern -> ANT_PATH_MATCHER.match(pattern, path));
    }

}
